package vcp.client.GUI.mvc.model;

import java.util.List;

import vcp.client.forms.ComplaintForm;
import vcp.client.interfaces.IObservable;
import vcp.client.request.FillComplaintRequest;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Complaint;
import vcp.entity.Customer;
import vcp.enums.ComplaintType.ComplaintStatus;

/**
 * 
 * @author VCP team
 *         <p>
 *         complaint model
 *         </p>
 * @see UIAbstractModel
 */
public class UIFillComplaintModel extends UIAbstractModel
{
	/** instance of user **/
	private Customer customer;

	public Customer getCustomer ()
	{
		return this.customer;
	}

	public void setCustomer ( Customer customer )
	{
		this.customer = customer;
	}

	/**
	 * fill complaint model with information
	 * 
	 * @param model
	 */
	public UIFillComplaintModel ( IObservable model )
	{
		super(model);
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * 
	 * @return customer complaint
	 */
	public Complaint getCustomerFirstComplaint ()
	{
		return this.customer.getComplaints().get(0);
	}

	/**
	 * 
	 * @return list of all customer complaints
	 */
	public List <Complaint> getCustomerComplaints ()
	{
		return this.customer.getComplaints();
	}

	/**
	 * apply new complaint form
	 * 
	 * @param form
	 */
	public void applyComplaintForm ( ComplaintForm form )
	{
		FillComplaintRequest request = new FillComplaintRequest(this.customer, form);
		this.sendRequest(request, true);
	}

	/**
	 * check if customer have complaint
	 * 
	 * @return true if custome has complaints
	 */
	public boolean hasCustomerComplaints ()
	{
		return ! this.customer.getComplaints().isEmpty();
	}

	/**
	 * get complain
	 * 
	 * @param selected
	 * @return user complaint
	 */
	public Complaint getComplaint ( Integer selected )
	{
		Complaint foundComplaint = null;
		for ( Complaint complaint : this.customer.getComplaints() )
		{
			if ( complaint.getID().intValue() == selected )
			{
				foundComplaint = complaint;
				break;
			}
		}

		return foundComplaint;
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * update user balance with refund data
	 * 
	 * @param refundBalance
	 */
	private void updateCustomerBalance ( Float refundBalance )
	{
		this.customer.setBalance(this.customer.getBalance() + refundBalance.floatValue());
	}

	/**
	 * insert new information into complaint
	 * 
	 * @param complaint
	 */
	private void updateComplaintInfo ( Complaint complaint )
	{
		Complaint customerComplaint = this.getComplaint(complaint.getID());
		if ( customerComplaint == null )
		{
			this.getCustomerComplaints().add(complaint);
		}
		else
		{
			customerComplaint.setStatus(complaint.getStatus());
			customerComplaint.setComplaintResolve(complaint.getComplaintResolve());
			customerComplaint.setAmountRefound(complaint.getAmountRefound());
			customerComplaint.setResolveText(complaint.getResolveText());
			customerComplaint.setHistory(complaint.isHistory());
			customerComplaint.setSiteEnum(complaint.getSiteEnum());
		}
	}

	/****************************************************************************************************************************
	 *** UIAbstractModel methods ***
	 ****************************************************************************************************************************/
	@Override
	public void shouldRegisterForUpdates ()
	{
		this.model.registerForUpdates(this);
	}

	@Override
	public void clearModel ()
	{
		this.customer = null;
	}

	/****************************************************************************************************************************
	 *** IObserver methods ***
	 ****************************************************************************************************************************/
	@Override
	public void update ( Object data )
	{
		if ( data instanceof Complaint )
		{
			this.updateComplaintInfo((Complaint) data);

			this.controllerDelegate.updateControllerDelegateWithData(UserNotification.COMPLAINT_SUBMITED);
			this.controllerDelegate.updateControllerDelegateWithData(data);
			this.controllerDelegate.loadingStop();
		}
	}

	@Override
	public void updateWithReferesh ( Object data )
	{
		if ( data instanceof Complaint )
		{
			Complaint complaint = (Complaint) data;
			this.updateComplaintInfo(complaint);

			if ( complaint.getAmountRefound() != null )
			{
				Float complaintRefund = (float) complaint.getAmountRefound();
				if ( complaint.getStatus() == ComplaintStatus.CLOSED )
				{
					this.updateCustomerBalance(complaintRefund);
					String complaintInfo = "Complaint " + complaint.getID() + " was closed.\n" + UserNotification.SUBSCRIPTION_REFUND__RefundValue__;
					complaintInfo = String.format(complaintInfo, complaintRefund);
					this.controllerDelegate.updateControllerDelegateWithData(complaintInfo);
				}
			}

			this.controllerDelegate.updateControllerDelegateWithData(data);
		}
	}

}
